package cn.wolfcode.service.impl;

import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUtil;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.NoticeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.UtilContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper,Notice> implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void insert(Notice notice) {
        //设置创建人
        notice.setCreator(UtilContext.getCurrentUser());
        //设置创建时间
        notice.setCreatTime(new Date());
        //设置未发布状态
        notice.setStatus(false);
        //先往数据库添加公告
        noticeMapper.insert(notice);
        //往中间表插入数据
        //查所有员工的id
        List<Employee> employees = employeeMapper.selectList(null);
        for (Employee employee : employees) {
            Long eid = employee.getId();
            //往中间表设置公告初始
            noticeMapper.saveRead(notice.getId(), eid);
        }
    }

    @Override
    public void delete(Long id) {
        noticeMapper.deleteById(id);
    }

    @Override
    public void update(Notice notice) {
        noticeMapper.updateById(notice);
    }

    @Override
    public Notice get(Long id, Boolean read) {
        noticeMapper.updateRead(id, UtilContext.getCurrentUser().getId(), read);
        return noticeMapper.selectById(id);
    }

    @Override
    public List<Notice> listAll() {
        return noticeMapper.selectList(null);
    }

    @Override
    public IPage<Notice> query(QueryObject qo) {
        Page<Notice> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        QueryWrapper<Notice> wrapper = Wrappers.<Notice>query();
        return super.page(page,wrapper);
    }

    @Override
    public void updataStatus(Long id, boolean status) {
        noticeMapper.updataStatus(id, status);
    }

    @Override
    public int selectCount() {
        //获得当前用户的id
        Long eid = UtilContext.getCurrentUser().getId();
        return noticeMapper.selectCount(eid);
    }

    @Override
    public List<Notice> selectByIdNotice(Long eid) {
        List<Notice> notices = noticeMapper.selectByIdNotice(eid);
        Date date = new Date();
        for (Notice notice : notices) {
            String str = "";
            String between = DateUtil.formatBetween(notice.getCreatTime(), date, BetweenFormatter.Level.MINUTE);
            if (between.contains("天")) {
                str = between.substring(0, between.indexOf("天") + 1);
            } else if (between.contains("小时")) {
                str = between.substring(0, between.indexOf("小时") + 2);
            } else if (between.contains("分")) {
                str = between.substring(0, between.indexOf("分") + 1);
            }
            //未读公告的发布时间
            notice.setNoticeTime(str);
        }

        return notices;
    }


}
